package com.nondo.dean.platform.controller.api.admin;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.platform.domain.Slide;
import com.nondo.dean.platform.model.result.SlideResult;
import com.nondo.dean.platform.service.SlideService;

/**
 * 幻灯片管理后台接口
 * @author zlj
 * @date 2018年4月9日
 */
@RestController("apiAdminSlide")
@RequestMapping("/{v}/admin/slide")
public class SlideController {
    
    @Autowired
    SlideService slideService;
    
    /**
     * 查看幻灯片详情
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/info/{id}")
    public View<SlideResult> info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View<SlideResult> view = new View<SlideResult>(20000, "获取数据成功");
        
        SlideResult slideResult = new SlideResult();
        if (id != null) {
            Slide slideDb = slideService.selectSlide(id);
            slideResult = slideService.getSlideDetail(slideDb);
        }

        view.setData(slideResult);
        return view;
    }
    
    /**
     * 添加幻灯片
     * @param request
     * @param response
     * @param slide 幻灯片信息 {@link Slide}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<Slide> add(HttpServletRequest request, HttpServletResponse response,
            @Validated @RequestBody Slide slide, BindingResult result) {
        View<Slide> view = new View<Slide>(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            Slide slideDb = this.slideService.selectSlideByName(slide.getName());
            if (slideDb != null) {
                return View.of500Error("幻灯片名称已存在");
            }
            this.slideService.addSlide(slide);
        }
        return view;
    }

    /**
     * 更新幻灯片
     * @param request
     * @param response
     * @param slide 幻灯片信息 {@link Slide}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<Slide> update(HttpServletRequest request,
            HttpServletResponse response,
            @Validated @RequestBody Slide slide, BindingResult result) {
        View<Slide> view = new View<Slide>(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            Slide slideDb = slideService.selectSlide(slide.getId());
            if (slideDb.getName().equals(slide.getName()) && slideDb.getId() != slide.getId()) {
                return View.of500Error("幻灯片名称已存在");
            }
            BeanUtils.copy(slide, slideDb);
            this.slideService.updateSlide(slideDb);
        }

        return view;
    }

    /**
     * 获取幻灯片分页查询列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<SlideResult>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Slide> query) {
        View<Page<SlideResult>> view = new View<Page<SlideResult>>(20000, "获取列表成功");

        Slide slide = query.getData();

        Page<SlideResult> page = new Page<SlideResult>(query.getPage(), query.getSize(), slideService.count(slide));
        
        List<Slide> slideList = slideService.getSlideList(query.getOffset(), query.getLimit(), slide);
        
        List<SlideResult> slideResultList = new ArrayList<SlideResult>();
        
        for (Slide slideDb : slideList) {
            SlideResult slideResult = slideService.getSlideDetail(slideDb);
            slideResultList.add(slideResult);
        }
        
        page.setContent(slideResultList);
        
        view.setData(page);
        
        return view;
    }

    /**
     * 删除幻灯片
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @SuppressWarnings("rawtypes")
    @RequestMapping("/delete/{id}")
    public View delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View view = new View(20000, "删除成功");

        Slide slide = slideService.selectSlide(id);
        slide.setDelFlag(String.valueOf(1));
        this.slideService.updateSlide(slide);

        return view;
    }
    
    /**
     * 获取幻灯片列表
     * @param request
     * @param response
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list/all")
    public View<List<Slide>> list(HttpServletRequest request, HttpServletResponse response) {
        View<List<Slide>> view = new View<List<Slide>>(20000, "获取列表成功");

        List<Slide> slideList = slideService.getSlideList(null);
        
        view.setData(slideList);
        
        return view;
    }
}